package activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.liuyujia.activity.R;

import Fragment.ClassityFragment;
import Fragment.HomePageFragment;
import Fragment.CaseFragment;
import Fragment.SettingFragment;
import Fragment.CouponFragment;

/**
 * Created by liuyujia on 2015/4/10.
 */
public class FragmentMainActivity extends BaseActivity implements View.OnClickListener{
   private HomePageFragment homePageFragment;
   private ClassityFragment classityFragment;
   private CouponFragment couponFragment;
   private CaseFragment caseFragment;
   private SettingFragment settingFragment;
   private View account_layout;
   private View classity_layout;
   private View case_layout;
    private View coupon_layout;
   private View setting_layout;
   private ImageView account_image,deal_image,case_img,coupon_img,setting_image;
   private TextView tv_hyb,tv_deal_time,tv_coupon,tv_case,tv_setting;
   private FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_fragment_main);
        initViews();
        fragmentManager = getFragmentManager();
        setTabSelection(0);
    }
    private void initViews() {
        account_layout = findViewById(R.id.account_layout);
        classity_layout = findViewById(R.id.classity_layout);
        case_layout = findViewById(R.id.case_layout);
        coupon_layout=findViewById(R.id.coupon_layout);
        setting_layout = findViewById(R.id.setting_layout);
        account_image = (ImageView) findViewById(R.id.account_image);
        deal_image = (ImageView) findViewById(R.id.deal_image);
        case_img = (ImageView) findViewById(R.id.case_img);
        coupon_img=(ImageView) findViewById(R.id.coupon_img);
        setting_image = (ImageView) findViewById(R.id.setting_image);
        tv_hyb=(TextView)findViewById(R.id.tv_hyb);
        tv_deal_time=(TextView)findViewById(R.id.tv_deal_time);
        tv_coupon=(TextView)findViewById(R.id.tv_coupon);
        tv_case=(TextView)findViewById(R.id.tv_case);
        tv_setting=(TextView)findViewById(R.id.tv_setting);
        account_layout.setOnClickListener(this);
        classity_layout.setOnClickListener(this);
        case_layout.setOnClickListener(this);
        coupon_layout.setOnClickListener(this);
        setting_layout.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
         switch (v.getId()){
             case R.id.account_layout:
                 setTabSelection(0);
                 break;
             case R.id.classity_layout:
                 setTabSelection(1);
                 break;
             case R.id.coupon_layout:
                 setTabSelection(2);
                 break;
             case R.id.case_layout:
                 setTabSelection(3);
                 break;
             case R.id.setting_layout:
                 setTabSelection(4);
                 break;
         }
    }
    private void setTabSelection(int index) {
        clearSelection();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (index){
            case 0:
                account_image.setImageResource(R.drawable.glasses_main_home);
                tv_hyb.setTextColor(Color.parseColor("#ff9933"));
                if (homePageFragment==null){
                    homePageFragment=new HomePageFragment();
                    transaction.add(R.id.content,homePageFragment);
                }else{
                    transaction.show(homePageFragment);
                }
                break;
            case 1:
                deal_image.setImageResource(R.drawable.glasses_classed);
                tv_deal_time.setTextColor(Color.parseColor("#ff9933"));
                if(classityFragment==null){
                    classityFragment=new ClassityFragment();
                    transaction.add(R.id.content,classityFragment);
                }else {
                    transaction.show(classityFragment);
                }
                break;
            case 2:
                coupon_img.setImageResource(R.drawable.glasses_coupons);
                tv_case.setTextColor(Color.parseColor("#ff9933"));
                if (couponFragment==null){
                    couponFragment=new CouponFragment();
                    transaction.add(R.id.content,couponFragment);
                }else{
                    transaction.show(couponFragment);
                }
                break;
            case 3:
                case_img.setImageResource(R.drawable.glasses_cases);
                tv_coupon.setTextColor(Color.parseColor("#ff9933"));
                if (caseFragment==null){
                    caseFragment=new CaseFragment();
                    transaction.add(R.id.content,caseFragment);
                }else{
                    transaction.show(caseFragment);
                }
                break;
            case 4:
                setting_image.setImageResource(R.drawable.glasses_setting);
                tv_setting.setTextColor(Color.parseColor("#ff9933"));
                if (settingFragment==null){
                    settingFragment=new SettingFragment();
                    transaction.add(R.id.content,settingFragment);
                }else{
                    transaction.show(settingFragment);
                }
                break;
        }
               transaction.commit();

    }
    private void clearSelection(){
        account_image.setImageResource(R.drawable.glasses_main);
        tv_hyb.setTextColor(Color.WHITE);
        deal_image.setImageResource(R.drawable.glasses_class);
        tv_deal_time.setTextColor(Color.WHITE);
        coupon_img.setImageResource(R.drawable.glasses_coupon);
        tv_coupon.setTextColor(Color.WHITE);
        case_img.setImageResource(R.drawable.glasses_case);
        tv_case.setTextColor(Color.WHITE);
        setting_image.setImageResource(R.drawable.glasses_setup);
        tv_setting.setTextColor(Color.WHITE);
    }
    private void hideFragments(FragmentTransaction transaction){
        if (homePageFragment!=null){
            transaction.hide(homePageFragment);
        }if (couponFragment!=null){
            transaction.hide(couponFragment);
        }if (caseFragment!=null){
            transaction.hide(caseFragment);
        }if (classityFragment!=null){
            transaction.hide(classityFragment);
        }if (settingFragment!=null){
            transaction.hide(settingFragment);
        }
    }
}
